<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yangjidan/model')->方法名();
*/
namespace jy_yangjidan;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function upmember($mid=0,$fromuser=0){
		$mid=$mid?:$_SESSION['uid'];
		$member = pdo_get('jy_yangjidan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if(empty($member)){
			if(!$fromuser){
				$weishop_member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid),'fromuser');
				$fromuser=$weishop_member['fromuser']?:$_SESSION['fromuser'];
			}
			if($fromuser){
				$allfee0=$this->getyeji($fromuser,0);
				$allfee1=$this->getyeji($fromuser,1);
				$isa=$allfee0>$allfee1?1:0;
				$paimid=$this->getpaimid($fromuser,$isa);
				$isa=pdo_count('jy_yangjidan_member',array('paimid'=>$paimid,'uniacid'=>$_SESSION['uniacid']));
				$isa=$isa?1:0;
			}
			$membernew=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid' => $mid,
				'fromuser'=>$fromuser,
				'isa' => $isa,
				'paimid'=>$paimid,
				'createtime'=>time(),
			);
			pdo_insert('jy_yangjidan_member',$membernew);
			return $membernew;
		}
	}
	public function up_looklog($youmid=0){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isget=$this->pdo_get('looklog',array('youmid'=>$youmid,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isget){
			return false;
		}else{
			$looklog=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'youmid'=>$youmid,
				'createtime'=>time(),
			);
			pdo_insert('jy_yangjidan_looklog',$looklog);
			return true;
		}
	}
	public function jiahaoyou($youmid=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$youmid){
			return '请选择好友！';
		}
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isget=$this->pdo_get('haoyou',array('youmid'=>$youmid,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($isget){
			$this->delete('haoyou',array('youmid'=>$youmid,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			return '取消好友成功！';
		}else{
			$looklog=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$mid,
				'youmid'=>$youmid,
				'createtime'=>time(),
			);
			pdo_insert('jy_yangjidan_haoyou',$looklog);
			return '加好友成功！';
		}
	}
	public function upfeelog($post=array(),$type=0,$jiefee=0){
		$data=array(
			'uniacid' => $_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'sn'=>get_ordersn('jy_yangjidan_feelog','sn'),
			'jiid'=>$post['jiid'],
			'price'=>$post['price'],
			'total'=>$post['total']?:1,
			'jitype'=>$post['jitype'],
			'isall'=>$post['isall'],
			'type'=>$type,
			'jiefee'=>$jiefee,
			'createtime'=>time(),
		);
		pdo_insert('jy_yangjidan_feelog',$data);
		$id=pdo_insertid();
		return $id;
	}
	public function addmyji($order=array()){
		for($i=0;$i<$order['total'];$i++){
			$count=$this->pdo_count('myji',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
			$count++;
			$title=$order['jitype']?'养鸡'.$count.'号':'蛋鸡'.$count.'号';
			for($i=0;$i<$order['total'];$i++){
				$data=array(
					'uniacid' => $_SESSION['uniacid'],
					'mid'=>$order['mid'],
					'sn'=>get_ordersn('jy_yangjidan_myji','sn'),
					'feeid'=>$order['id'],
					'price'=>$order['price']/$order['total'],
					'title'=>$title,
					'jiefee'=>$order['jiefee'],
					'yudan'=>$this->setting['dj_yudan'],
					'lsyudan'=>$this->setting['dj_yudan'],
					'type'=>$order['jitype'],
					'days'=>$order['jitype']?$this->setting['dj_allday2']:$this->setting['dj_allday'],
					'isweitou'=>$order['isall'],
					'status'=>1,
					'weitime'=>time()-24*3600,
					'createtime'=>time(),
				);
				pdo_insert('jy_yangjidan_myji',$data);
				$data['id']=pdo_insertid();
			}
			$this->addjilog($data,0);
			if(!$data['jiefee']){
				$this->model('cv')->up_renwu($data,1);
				$this->model('cv')->up_renwu($data,3);
				$this->model('cv')->up_renwu($data,3);
			}
			model('jy_area_psi/member')->update_agent_level($order['mid']);
		}
		return true;
	}
	public function addjilog($myji=array(),$type=0){
		$data=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$myji['mid'],
				'jiid'=>$myji['id'],
				'msg'=>$this->jilog_type[$type],
				'price'=>$myji['fee']?:$myji['price'],
				'type'=>$type,
				'jitype'=>$myji['jitype'],
				'createtime'=>time(),
		);
		if($type==7){
			$data['koufee']=$this->setting['dj_koufee']*$data['price']/100;
		}
		pdo_insert('jy_yangjidan_jilog',$data);
		if($type==4){
			pdo_update('jy_yangjidan_myji',array('jdtime'=>time(),'jd +='=>1,'lsyudan -='=>1),array('id'=>$myji['id'],'uniacid'=>$_SESSION['uniacid']));
			pdo_update('jy_yangjidan_member',array('jd +='=>1),array('mid'=>$myji['mid'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($type==5){
			pdo_update('jy_yangjidan_myji',array('jianjdtime'=>time(),'lsyudan -='=>$this->setting['dj_tuiday']),array('id'=>$myji['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($type==3){
			pdo_update('jy_yangjidan_myji',array('status'=>1,'weitime'=>time()),array('id'=>$myji['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function toweiji($jiid=0){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$dj_dayfee=$this->setting['dj_dayfee'];
		if($jiid){
			$myji=$this->pdo_get('myji',array('mid'=>$_SESSION['youmid']?:$_SESSION['uid'],'id'=>$jiid,'uniacid'=>$_SESSION['uniacid']));
			if($myji['isweitou']){
				exi('该【'.$myji['title'].'】蛋鸡目前已委托投喂！','error');
			}
			if($myji['status']!=1 && $myji['status']!=2){
				exi('该【'.$myji['title'].'】蛋鸡目前的状态不能喂养！','error');
			}
			if($myji['weitime']>$daytime){
				exi('该【'.$myji['title'].'】蛋鸡今天已经喂养过了！无需再次喂养！','error');
			}
			$lsfee=$dj_dayfee;
		}else{
			$noweiji=$this->pdo_count('myji',array('isweitou'=>0,'status'=>1,'mid'=>$_SESSION['youmid']?:$_SESSION['uid'],'weitime <'=>$daytime,'uniacid'=>$_SESSION['uniacid']));
			$lsfee=$noweiji*$dj_dayfee;
		}
		if(!$jiefee){
			$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','mobile','avatar','nickname'));
			if($member['credit2']<$lsfee){
				exi('当前余额仅剩【'.$member['credit2'].'】！请先充值！','error');
			}
			model('core/member')->member_credit($_SESSION['uid'],'credit2',$lsfee,false,'jy_yangjidan','喂养小鸡');
		}
		$update=array('status'=>1,'weitime'=>time(),'lsfee +='=>$lsfee,'lsday +='=>1,'days -='=>1);
		if($jiid){
			$this->pdo_update('myji',$update,array('id'=>$jiid,'uniacid'=>$_SESSION['uniacid']));
			$myji=$this->pdo_get('myji',array('id'=>$jiid,'uniacid'=>$_SESSION['uniacid']));
			$myji['fee']=$lsfee;
			$myji['jifefee']=$jifefee;
			$this->addjilog($myji,1);
		}else{
			$list = pdo_getall('jy_yangjidan_myji',array('isweitou'=>0,'status'=>1,'mid'=>$_SESSION['youmid']?:$_SESSION['uid'],'weitime <'=>$daytime,'uniacid'=>$_SESSION['uniacid']));
			foreach($list as $item){
				$this->pdo_update('myji',$update,array('status'=>array(1,2),'id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['fee']=$dj_dayfee;
				$item['jifefee']=$jifefee;
				$this->addjilog($item,1);
			}
		}
		return '喂养成功~';
	}
	public function addjimai($post=array()){
		//$this->checkjiefee();
		$member = pdo_get('jy_yangjidan_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		$lscv=$this->model('cv')->get_lscv();
		if($this->setting['cv_saleji']>$member['cv']+$lscv){
			exi('可用贡献值不足，不能寄卖鸡蛋！','error');
		}
		$this->model('cv')->jian_cv($this->setting['cv_saleji'],'销售鸡蛋消耗',10);
		$dj_huigouday=$this->setting['dj_huigouday'];
		$koufee=$this->setting['dj_koufee']*$post['jmprice']/100;
		$data=array(
			'uniacid' => $_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'sn'=>get_ordersn('jy_yangjidan_jimai','sn'),
			'type'=>$post['type'],
			'jiid'=>$post['maijiid']?:$post['jiid'],
			'jdnum'=>$post['jmdan'],
			'price'=>$post['jmprice'],
			'koufee'=>$koufee,
			'status'=>1,
			'endtime'=>time()+$dj_huigouday*24*3600,
			'createtime'=>time(),
		);
		pdo_insert('jy_yangjidan_jimai',$data);
		if(!$post['type']){
			$this->addjilog($data,6);
			pdo_update('jy_yangjidan_member',array('jd -='=>$post['jmdan']),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$this->addjilog($data,9);
			pdo_update('jy_yangjidan_myji',array('status'=>5),array('id'=>$data['jiid'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function myjiorder($jimai=array()){
		unset($jimai['address']['lat_lng']);
		unset($jimai['address']['address2']);
		$data=array(
			'uniacid' => $_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('jy_yangjidan_myjiorder','ordersn'),
			'price'=>$jimai['price'],
			'jifen2'=>$jimai['jifen2'],
			'jimaiid'=>$jimai['id'],
			'jdnum'=>$jimai['jdnum'],
			'fmid'=>$jimai['mid'],
			'type'=>$jimai['type'],
			'istihuo'=>$jimai['istihuo'],
			'name'=>$jimai['address']['name'],
			'mobile'=>$jimai['address']['mobile'],
			'address'=>ijson_encode($jimai['address']),
			'createtime'=>time(),
		);
		if($jimai['price']<=0 && $data['jimaiid']){
			$data['status']=1;
			pdo_update('jy_yangjidan_jimai',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$data['jimaiid']));
			if($jimai['jifen2']>0){
				pdo_update('jy_yangjidan_member',array('jifen2 -='=>$jimai['jifen2']),array('mid'=>$data['mid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		pdo_insert('jy_yangjidan_myjiorder',$data);
		$id=pdo_insertid();
		return $id;
	}
	public function huigou($jimai=array()){
		pdo_update('jy_yangjidan_jimai',array('status'=>3),array('uniacid' => $_SESSION['uniacid'],'id'=>$jimai['id']));
		$jimai['id']=0;
		$this->addjilog($jimai,7);
		return true;
	}
	public function jiesuan(){
		$jiesuanday=time()-7*24*3600;
		$price = pdo_getsum('jy_yangjidan_jilog',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'type'=>7,'status'=>0,'createtime <'=>$jiesuanday),'price');
		$koufee = pdo_getsum('jy_yangjidan_jilog',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'type'=>7,'status'=>0,'createtime <'=>$jiesuanday),'koufee');
		if($price>0){
			$credit2=$this->addjifen2($_SESSION['uid'],$price-$koufee);
			model('core/member')->member_credit($_SESSION['uid'],'credit2',$credit2,true,'jy_yangjidan','余额结算');
		}
		pdo_update('jy_yangjidan_jilog',array('status'=>2),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'type'=>7,'status'=>0,'createtime <'=>$jiesuanday));
		return true;
	}
	public function order_receive($orderid=0){
		$order=pdo_get('jy_yangjidan_myjiorder',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		$koufee = pdo_getcolumn('jy_yangjidan_jimai',array('id'=>$order['jimaiid'],'uniacid'=>$_SESSION['uniacid']),'koufee');
		$order['price']=$order['price']+$order['jifen2']-$koufee;
		$credit2=$this->addjifen2($order['fmid'],$order['price']);
		$jimai=array(
			'mid'=>$order['fmid'],
			'fee'=>$credit2,
		);
		$this->addjilog($jimai,8);
		model('core/member')->member_credit($order['fmid'],'credit2',$credit2,true,'jy_yangjidan','余额结算');
		pdo_update('jy_yangjidan_myjiorder',array('status'=>3),array('uniacid' => $_SESSION['uniacid'],'id'=>$orderid));
		return true;
	}
	public function addjifen2($mid=0,$orderprice=0){
		$dj_jinfen2=$this->setting['dj_jinfen2'];
		$jifen2=$dj_jinfen2*$orderprice/100;
		pdo_update('jy_yangjidan_member',array('jifen2 +='=>$jifen2),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return $orderprice-$jifen2;
	}
	public function checkjiefee($mid=0,$isget=false){
		$mid=$mid?:$_SESSION['uid'];
		$jifee=pdo_getsum('jy_yangjidan_feelog',array('status'=>0,'jiefee'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
		if($jifee>0 && !$isget){
			exi('需先还清赊账【￥'.$jifee.'】，才能寄售！','error');
		}
		return $jifee;
	}
	public function uppaimid($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$members = pdo_getall('jy_yangjidan_member',array('paimid'=>0,'uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid));
		foreach($members as $mv){
		}
	}
	//获取上级公位
	public function getpaimid($pidmid=0,$isa=0){
		$num=2;
		for($i=0;$i<=100;$i++){
			$isnum=pdo_count('jy_yangjidan_member',array('paimid'=>$pidmid,'uniacid'=>$_SESSION['uniacid']));
			if($isnum<$num){
				return $pidmid;
			}else{
				$allfee0=$this->getyeji($pidmid,0);
				$allfee1=$this->getyeji($pidmid,1);
				$isa=$allfee0>$allfee1?1:0;
				$pidmid=pdo_getcolumn('jy_yangjidan_member',array('isa'=>$isa,'paimid'=>$pidmid,'uniacid'=>$_SESSION['uniacid']),'mid');
			}
		}
		return false;
	}
	public function getyeji($mid=0,$isa=0,$starttime=0){
		$teamids=$this->getteamids($mid,$isa);
		$params=array('mid'=>$teamids,'status'=>1,'type'=>0,'uniacid'=>$_SESSION['uniacid']);
		$params2=array('mid'=>$teamids,'uniacid'=>$_SESSION['uniacid']);
		if($starttime){
			$yuetime= mktime(0,0,0,date('m'),1,date('Y'));
			$params['createtime >']=$starttime;
			$params2['createtime >']=$starttime;
		}
		$allfee=pdo_getsum('jy_yangjidan_feelog',$params,'price');
		$allgoodsfee=pdo_getsum('jy_area_psi_order',$params2,'order_psi');
		return $allfee+$allgoodsfee;
	}
	public function getteamids($teammid=0,$isa=0){
		$teammid=$teammid?:$_SESSION['uid'];
		$mid=pdo_getcolumn('jy_yangjidan_member',array('isa'=>$isa,'uniacid'=>$_SESSION['uniacid'],'paimid'=>$teammid),'mid');
		if(!$mid){
			return -1;
		}
		$newmembers=array($mid=>$mid);
		$paimid=$mid;
		for($i=1;$i<100;$i++){
			if(!$paimid){
				break;
			}
			$members=pdo_getall('jy_yangjidan_member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$paimid),array('mid'),'mid');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$paimid=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=-1;
		}
		return $teamids;
	}
	//一层
	public function getonepaimids($mid=0){
		if(!$mid){
			return array();	
		}
		$children=pdo_getall('jy_yangjidan_member',array('paimid'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('mid'));
		foreach($children as &$cv){
			$cv['name']=pdo_getcolumn('core_members',array('id'=>$cv['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
			$cv['name']=$cv['name']?:'无昵称';
			$cv['name'].='('.$cv['mid'].')';
		}
		return $children;
	}
	//获取我的关系图
	public function getmypaimids($mid=0,$isda=0){
		$allfee0=$this->getyeji($mid,0);
		$allfee1=$this->getyeji($mid,1);
		$isa=0;
		if(($isda && $allfee0<$allfee1) || (!$isda && $allfee0>$allfee1)){
			$isa=1;
		}
		if($allfee0==$allfee1){
			$isa=$isda;
		}
		$down=pdo_get('jy_yangjidan_member',array('isa'=>$isa,'paimid'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('mid'));
		$down['name']=pdo_getcolumn('core_members',array('id'=>$down['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
		$down['name']=$down['name']?:'无昵称';
		$down['name'].='('.$down['mid'].')';
		$childrens[]=$down;
		foreach($childrens as &$cv){
			$cv['children']=$this->getonepaimids($cv['mid']);
			foreach($cv['children'] as &$cv2){
				$cv2['children']=$this->getonepaimids($cv2['mid']);
				foreach($cv2['children'] as &$cv3){
					$cv3['children']=$this->getonepaimids($cv3['mid']);
					foreach($cv3['children'] as &$cv4){
						$cv4['children']=$this->getonepaimids($cv4['mid']);
						foreach($cv4['children'] as &$cv5){
							$cv5['children']=$this->getonepaimids($cv5['mid']);
							foreach($cv5['children'] as &$cv6){
								$cv6['children']=$this->getonepaimids($cv6['mid']);
								foreach($cv6['children'] as &$cv7){
									$cv7['children']=$this->getonepaimids($cv7['mid']);
									foreach($cv7['children'] as &$cv8){
										$cv8['children']=$this->getonepaimids($cv8['mid']);
										foreach($cv8['children'] as &$cv9){
											$cv9['children']=$this->getonepaimids($cv9['mid']);
											foreach($cv9['children'] as &$cv10){
												$cv10['children']=$this->getonepaimids($cv10['mid']);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return $childrens;
	}
}
?>